const Utils = ecodeSDK.imp(Utils)

const {
	isBlank,
	getForm,
	forEachDetail
} = Utils

const GField = {
	fieldInfoMap: new Map(),
	fieldNameMap: {},
	attrMap: {},

	getFieldId(fieldMark) {
		if (isBlank(fieldMark)) return ''
		return /field\d/.test(fieldMark) ?
			fieldMark : this.convertFieldNameToId(fieldMark)
	},

	proxyFieldComp(fieldMark, Com, props) {
		if (!fieldMark || !Com) return;
		const {fieldId, fieldid} = this.getFieldInfo(fieldMark)
		const form = getForm();
		const fid = WfForm ? fieldid : fieldId
		form.proxyFieldContentComp(fid, ({ rowIndex, fieldid }) => {
			props = {
				...props,
				fieldMark: fieldid,
				rowIdx: Number(rowIndex),
				idx: Number(rowIndex)
			}
			return <Com {...props} />
		})
		form.forceRenderField(fieldId);
	},

	getFieldInfo(fieldMark) {
		// 判断缓存中是否存在
		let info = this.fieldInfoMap.get(fieldMark)
		if (info !== undefined) return info
		const fieldId = this.getFieldId(fieldMark)
		if (!fieldId) {
			// 字段不存在, 进入缓存黑名单
			this.fieldInfoMap.set(fieldMark, undefined)
			return {}
		}
		const { WfForm, ModeForm } = window
		if (WfForm) {
			// 判断流程表单
			const id = fieldId.replace('field', '')
			info = WfForm.getFieldInfo(id)
		}
		if (ModeForm) {
			// 判断建模表单
			info = ModeForm.getFieldInfo(fieldId)
			const { isdetail, detail } = info
			info.isDetail = isdetail || false
			info.tableMark = isdetail ? detail : 'main'
		}
		if (info) {
			const {isDetail, tableMark} = info
			if (isDetail) {
				info.detailId = tableMark.replace('detail_', '')
			}
			info.fieldId = fieldId
			// 写入缓存并返回
			this.fieldInfoMap.set(fieldMark, info)
			return info
		}
	},

	convertFieldNameToId(fieldMark) {
		if (isBlank(fieldMark)) return ''
		let fm = this.fieldNameMap[fieldMark]
		if (fm !== undefined) return fm
		// 转化为大写
		let _field = fieldMark.toUpperCase();
		let pos, key = `main.${_field}`, isDetail = false;
		// 判断是否是明细字段
		if (_field.indexOf('D') === 0 && (pos = _field.indexOf('.')) !== -1) {
			key = _field.replace('D', 'detail_')
			_field = _field.substr(pos + 1)
			isDetail = true
		}
		const map = this.getFieldNameMap()
		let fid = map.get(key)
		if (isDetail && !fid) {
			// 参数=d.字段名
			let hasNext = true
			map.forEach((id, fname) => {
				if (hasNext && fname.startsWith('detail')
					&& fname.endsWith(`.${_field}`)) {
					fid = id
					hasNext = false
				}
			})
		}
		fm = fid ? `field${fid}` : ''
		this.fieldNameMap[fieldMark] = fm
		return fm
	},

	getFieldValue(fieldMark, rowIndex = null){
		if (isBlank(fieldMark)) return ''
		const {isDetail, detailId, fieldId} = this.getFieldInfo(fieldMark)
		const form = getForm()
		if (!isDetail) return form.getFieldValue(fieldId)
		if (rowIndex !== null) {
			return form.getFieldValue(`${fieldId}_${rowIndex}`)
		}
		let l = []
		forEachDetail(detailId, idx => {
			l.push(form.getFieldValue(`${fieldId}_${idx}`))
		})
		return l.join(',')
	},

	getFieldNum(fieldMark, rowIndex = null) {
		const {isDetail, detailId, fieldId} = this.getFieldInfo(fieldMark)
		const form = getForm()
		if (!isDetail) {
			// 主表字段
			const value = form.getFieldValue(fieldId)
			return Number.parseFloat(value) || 0
		}
		if (rowIndex !== null) {
			// 明细表单字段
			const value = form.getFieldValue(`${fieldId}_${rowIndex}`)
			return Number.parseFloat(value) || 0
		}
		// 明细表列合计
		let value = 0
		forEachDetail(detailId, idx => {
			const tmp = form.getFieldValue(`${fieldId}_${idx}`) || 0
			value += Number.parseFloat(tmp)
		})
		return value
	},

	getFieldNameMap: () => {
		const { WfForm, ModeForm } = window
		if (WfForm) return WfForm.getLayoutStore().fieldNameMap
		if (ModeForm) return cubeStore.Card.fieldNameMap
	}
}

ecodeSDK.exp(GField)